package com.zhuanzhuan.hero.user.helper;

import com.bj58.youpin.common.rpc.result.Result;
import com.bj58.youpin.common.utils.json.JsonUtils;
import com.zhuanzhuan.hero.biz.common.util.WxWarnTemplateUtil;
import com.zhuanzhuan.hero.user.consts.Consts;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero_message.facade.IHeroPushFacade;
import com.zhuanzhuan.hero_message.module.HeroSendPushRequestDTO;
import com.zhuanzhuan.youpin.log.interceptor.LogContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName PushHelper
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/22 14:08
 */
@Component
@Slf4j
public class PushHelper {

    /**
     * 靠谱认证
     */
    public final static Integer RELIABLE_AUTH_TEMPLATE_ID = 1004;
    /**
     * 同行认证
     */
    public final static Integer FRATERNITY_AUTH_TEMPLATE_ID = 1005;
    /**
     * 个人信息风控
     */
    public final static Integer BASE_INFO_TEMPLATE_ID = 2015;
    /**
     * 联系信息风控
     */
    public final static Integer CONTACT_INFO_TEMPLATE_ID = 2016;
    /**
     * 经营标签风控
     */
    public final static Integer MANAGEMENT_LABEL_TEMPLATE_ID = 2017;
    /**
     * 端外信息风控
     */
    public final static Integer OUTER_INFO_TEMPLATE_ID = 2018;
    /**
     * 同行认证标签风控
     */
    public final static Integer FRATERNITY_AUTH_RISK_TEMPLATE_ID = 2019;
    /**
     * 背景图风控
     */
    public final static Integer BACKDROP_TEMPLATE_ID = 2020;

    /**
     * 运营审核通过/驳回的push
     */
    public final static Integer AUDIT_REJECT_TEMPLATE_ID = 2026;
    public final static Integer AUDIT_PASS_TEMPLATE_ID = 2027;

    @Resource
    private IHeroPushFacade heroPushFacade;

    /**
     * 发送push
     *
     * @param receiverId
     * @param templateId
     * @param paramMap
     * @return void
     * @author  hanjunjun
     * @date 2024/7/11
     **/
    public void sendPush(Long receiverId, Integer templateId, Map<String, String> paramMap) {
        HeroSendPushRequestDTO sendPushRequestDTO = new HeroSendPushRequestDTO();
        sendPushRequestDTO.setReceiverId(receiverId);
        sendPushRequestDTO.setTemplateId(templateId);
        sendPushRequestDTO.setTemplateDataMap(paramMap);

        log.info("发送push request={}", JsonUtils.toJsonWithoutNull(sendPushRequestDTO));
        Result<Boolean> result = heroPushFacade.sendPush(LogContext.getLogStr(), sendPushRequestDTO);
        log.info("发送push result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("sendPush返回失败");
            WxWarnTemplateUtil.warnOutInvokedService(Consts.SERVICE_NAME, Consts.MESSAGE_SERVICE_NAME, "sendPush调用失败");
            throw new BusinessException("sendPush返回失败");
        }

    }

    public static Map<String, String> parseParamMapByField(String fieldValue) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("filedName", fieldValue);
        return paramMap;
    }
}
